﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using SportsStore.Model;
using SportsStore.ServicesInterfaces;
using SportsStore.Web.Controllers;
using SportsStore.Web.Infrastructure;
using SportsStore.Web.Models.User;

namespace SportsStore.Web.Filters
{
    public class GetCookieUserFilterAttribute : AuthorizeAttribute
    {
        public const string UserCookieName = "USER";

        public IUserService UserService { get; set; }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var controller = filterContext.Controller as BaseController;
            if (controller != null)
            {
                var userViewModel = new UserViewModel { IsLoggedIn = false };
                if (filterContext.HttpContext.Request.Cookies[UserCookieName] != null)
                {
                    string cookie = filterContext.HttpContext.Request.Cookies[UserCookieName].Value;
                    byte[] cipherText = Convert.FromBase64String(cookie);
                }
                filterContext.HttpContext.User = Thread.CurrentPrincipal = userViewModel;
            }
        }
    }
}